package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.ZyBuilding;
import com.blb.community_admin.entity.ZyCommunity;
import com.blb.community_admin.service.IZyBuildingService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Api("")
@RestController
public class ZybuildingController {
    @Autowired
    private IZyBuildingService buildingService;

    /**
     * 查询所有楼栋信息
     *
     * @return
     */
    @ApiOperation("查询所有楼栋信息")
    @GetMapping("/zybuildings")
    public ResponseResult<List<ZyBuilding>> selectBuilding() {
        List<ZyBuilding> list = buildingService.selectBuilding();
        return ResponseResult.ok(list);
    }

    /**
     * 模糊查询分页
     *
     * @param current
     * @param size
     * @param
     * @param
     * @return
     */
    @ApiOperation("模糊查询分页")
    @PostMapping("/building/page")
    public ResponseResult<IPage<ZyBuilding>> selectBuildingPage(Long current, Long size, @RequestBody ZyBuilding zyBuilding) {
        System.out.println(zyBuilding);
        IPage<ZyBuilding> zyBuildingIPage = buildingService.selectBuildingPage(current,size,zyBuilding);
        return ResponseResult.ok(zyBuildingIPage);
    }

    /**
     * 添加楼栋
     *
     * @param zyBuilding
     * @return
     */
    @Log(module = "楼栋", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加楼栋")
    @PostMapping("/building")
    public ResponseResult<String> addBuilding(@RequestBody ZyBuilding zyBuilding) {
        zyBuilding.setCreateTime(LocalDateTime.now());
        buildingService.save(zyBuilding);
        return ResponseResult.ok("添加成功");
    }

    /**
     * 修改楼栋
     *
     * @param zyBuilding
     * @return
     */
    @Log(module = "楼栋", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改楼栋")
    @PutMapping("/building")
    public ResponseResult<String> updateBuilding(@RequestBody ZyBuilding zyBuilding) {
        buildingService.updateById(zyBuilding);
        return ResponseResult.ok("修改成功");
    }

    /**
     * 删除楼栋
     *
     * @param id
     * @return
     */
    @Log(module = "楼栋", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除楼栋")
    @DeleteMapping("/building/{id}")
    public ResponseResult<String> deleteBuilding(@PathVariable long id) {
        buildingService.removeById(id);
        return ResponseResult.ok("删除成功");
    }

    /**
     * 多删
     *
     * @param ids
     * @return
     */
    @Log(module = "楼栋", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("多删")
    @PostMapping("/building/ids")
    public ResponseResult<String> deleteBuilding(@RequestBody String[] ids) {
        System.out.println(ids);
        List<String> list = Arrays.asList(ids);
        buildingService.removeByIds(list);
        return ResponseResult.ok("删除成功");
    }

    /**
     * 获取小区信息
     *
     * @return
     */
    @ApiOperation(value = "获取小区", notes = "用于获取小区信息")
    @GetMapping("/getAllCommunity")
    public List<ZyCommunity> getAllCommunity() {
        return buildingService.getAllCommunity();
    }

    @Log(module = "楼栋", operationType = 5, requestMethod = "POST")
    @ApiOperation("导出")
    @PostMapping("/building/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<ZyBuilding> zyBuildings) throws IOException {
        String path = EasyExcelUtil.write("楼栋数据", ZyBuilding.class, zyBuildings);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }

}
